import {calculateMatchDuration} from "@/utils/date.js"
interface MatchState {
    status : number,
    label : string,
    color : string,
    start(): void;
    end(): void;
}

class NotStartedState implements MatchState {

    color = "blue";
    label = "开始倒计时";
    status = 0;

    constructor(private match: Match) {

    }

    start(): void {
      //  console.log("比赛开始");
        this.match.setState(new InProgressState(this.match));
    }

    end(): void {
        //console.log("比赛还未开始");
    }


}

class InProgressState implements MatchState {

    color = "green";
    label =  "结束倒计时";
    status =  1;

    constructor(private match: Match) {}

    start(): void {
        console.log("比赛已经开始");
    }

    end(): void {
        console.log("比赛结束");
        this.match.setState(new EndedState(this.match));
    }


}

class EndedState implements MatchState {

    color =  "red";
    label =  "比赛已结束";
    status =  2;

    constructor(private match: Match) {}

    start(): void {
        console.log("比赛已经结束");
    }

    end(): void {
        console.log("比赛已经结束");
    }

}

export default class Match {
    /**
     * 当前状态
     * @private
     */
    private state: MatchState;

    /**
     * 距离下个状态的倒计时
     * @private
     */
    private countdown : string;

    /**
     * 进度
     * @private
     */
    private progress : string;

    /**
     * 定时器
     * @private
     */
    public timer : number;


    constructor(private startTime: Date, private endTime: Date) {
        this.state = new NotStartedState(this);
    }

    public destroy() {
        this.removeInterval();
    }

    /**
     * 主入口，创建一个比赛状态实例
     * @param startTime 比赛开始时间
     * @param endTime 比赛结束时间
     */
    public static createContestStatus(startTime: Date, endTime: Date) : Match{
        const match = new Match(startTime, endTime);
        match.updateState();
        return match;
    }

    /**
     * 定时改变状态
     * @param match
     */
    public static toInterval(match : Match){
        match.timer = setInterval(() => {
            match.updateState();
            if(match.state instanceof EndedState){
                clearInterval(match.timer); // 停止计时器
            }
        }, 1000);
    }

    /**
     * 删除定时器
     */
    public removeInterval(){
        clearInterval(this.timer); // 停止计时器
    }


    public setState(state: MatchState): void {
        this.state = state;
    }

    public updateState(): void {
       // console.log(6666)
        const now = new Date();
        this.updateProgress(now);
        if (now < this.startTime) { //比赛未开始
            this.countdown = calculateMatchDuration(now,this.startTime);
            if(!(this.state instanceof NotStartedState))
                this.setState(new NotStartedState(this));
        } else if (now >= this.startTime && now <= this.endTime) { //比赛进行中
            this.countdown = calculateMatchDuration(now,this.endTime);
            if(!(this.state instanceof InProgressState))
                this.setState(new InProgressState(this));
        } else { //比赛已结束
            this.countdown = "";
            if(!(this.state instanceof EndedState))
                this.setState(new EndedState(this));
        }
        // console.log(this.state)
        // console.log(this.countdown)
    }

    public start(): void {
        this.state.start();
    }

    public end(): void {
        this.state.end();
    }

    public getState() : MatchState{
        return this.state;
    }

    public isInProgressState() : boolean{
        return this.state instanceof InProgressState;
    }

    public isNotStartedState() : boolean{
        return this.state instanceof NotStartedState;
    }

    public getProgress() : string{
       // if(this.isInProgressState()){
       //     // 转换为毫秒级时间戳
       //     let currentTime = new Date().getTime();
       //     let startTime = this.startTime.getTime();
       //     let endTime = this.endTime.getTime();
       //     return (((currentTime - startTime) / (endTime - startTime)) * 100).toFixed(5);
       // }
       return this.progress;
    }

    public updateProgress(now : Date) : void{
        if(this.isInProgressState()){
            // 转换为毫秒级时间戳
            let currentTime = now.getTime();
            let startTime = this.startTime.getTime();
            let endTime = this.endTime.getTime();
            this.progress =  (((currentTime - startTime) / (endTime - startTime)) * 100).toFixed(2);
        }
    }


    public getCountdown() : string{
        return this.countdown;
    }
}





